let $;
layui
    .config({
        base: "../js/lib/",
    })
    .use(["form", "CryptoJS", "util"], function () {
        let form = layui.form, CryptoJS = layui.CryptoJS;
        let util = layui.util;
        $ = layui.$;
        const LOGINKEY = "DagV2";
        $("#member").val(localStorage.getItem("member"));
        $("#account").val(localStorage.getItem("account"));
        getValidateCode();

        var csrfToken = getCsrfToken();


        console.log(csrfToken)
        form.on("submit(postData)", function (obj) {
            let data = obj.field;
            data["password"] = CryptoJS.MD5(PM_CHECK_CODE + CryptoJS.MD5(data["password"]).toString()).toString();
            data["key"] = localStorage.getItem(LOGINKEY);
            $.ajax({
                url: "./userLogin", //url
                contentType: "application/json",
                dataType: "json", //数据格式
                type: "POST", //请求方式
                async: false, //是否异步请求
                data: JSON.stringify(data),
                success: function (result) {
                    if (result.code === 0) {
                        localStorage.setItem("account", data.account);
                        localStorage.setItem("login_user", result.data.userName + "(" + data["account"] + ")");
                        sessionStorage.setItem("token", result.data.spm);
                        sessionStorage.setItem("spm", data["key"]);
                        $.ajaxSetup({
                            headers: {
                                'spm': data["key"],
                                'token': result.data.spm
                            }
                        });
                        window.location.href = result.data.url;
                    } else {
                        layer.alert(result.message, {icon: 2});
                        getValidateCode();
                    }
                },
            });
            return false;
        });

        util.on("lay-on", {
            getValidateCode: function () {
                getValidateCode();
            },
            userRegister: function () {
                top.layer.open({
                    type: 2,
                    title: '用户注册',
                    content: './register',
                    area: ['550px', '400px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        var iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            layer.close(index);
                        }
                        return false;
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
            }

        });

        function getValidateCode() {
            $.ajax({
                method: 'get',
                url: './validateCode?' + Math.random(),
                success: function (res) {
                    let url = res.data.code;
                    localStorage.setItem(LOGINKEY, res.data.key);
                    $("#verifyCode").attr("src", url);
                },
                error: function (e) {
                    console.log(e);
                },
            })
        }

        // 从 Cookie 中获取 Token（假设使用默认的 Cookie 名称）
        function getCsrfToken() {
            const cookies = document.cookie.split(';');
            for (let cookie of cookies) {
                const [name, value] = cookie.trim().split('=');
                if (name === 'XSRF-TOKEN') {
                    return decodeURIComponent(value);
                }
            }
            return '';
        }


    });


